home *** CD-ROM | disk | FTP | other *** search
- /* file "misc_utils.c" -- miscellaneous utilities for the qndxr project...
- * by ^z -- 870821-...
- */
-
-
- #include <stdio.h>
- #include <unix.h>
- #include <storage.h>
- #include <ctype.h>
- #include <proto.h>
- #include "qndxr.2.h"
-
- /* this function returns a value for the size of the internal buffers,
- * zbufsiz, and it also takes care of setting the other global parameters,
- * keep_all_punct, keep_embedded_punct, and keep_special_chars,
- * which govern how punctuation and special characters are handled.
- * They are set based on flags such as -e, -a, and -s in the input line.
- * The input parameters are taken one after another, and any that do
- * not convert to a nonzero number are scanned for the letters "e", "a", and
- * "s". If a parameter is not reset, the default is to leave keep_all_punct,
- * keep_embedded_punct, and keep_special_chars as FALSE.
- * The default memory size is DEFAULT_MEMSIZ, set in the header file.
- */
-
- long set_params (argc, argv)
- int argc;
- char *argv[];
- {
- int i;
- void set_parser_options();
- long zb = 0, n, atol(), set_zbufsiz();
-
- for (i = 2; i < argc; ++i)
- {
- n = atol (argv[i]);
- if (n != 0)
- zb = set_zbufsiz (n);
- else
- set_parser_options (argv[i]);
- }
-
- if (zb == 0)
- zb = set_zbufsiz (DEFAULT_MEMSIZ);
-
- return (zb);
- }
-
-
-
- /* determine how big we should make the big buffers -- want to be sure
- * to have room for at least 2*NMERGE+2 of them at one time in memory,
- * so that the N-way merge function can hold all the input files
- * (2N) plus the output files (2).
- *
- * NOTE that the chosen buffer size must be a multiple of both sizeof(long)
- * and sizeof(KEY_REC); I ensure that very simply in what follows by
- * a little modular arithmetic. I also take care of the sign and of the
- * requirement that the buffer size be non-zero -- thus, UNIX aficionados
- * can precede the parameter with a "-" with no ill effects. I have put in
- * various safeguards against picking illegal buffer sizes, along with an
- * ultimate safety net small minimum value for zbufsiz.
- */
-
- long set_zbufsiz (zb)
- long zb;
- {
- char *testb;
-
- if (zb < 0)
- zb = -zb;
-
- zb /= (2 * NMERGE + 2);
- zb = zb - zb % (sizeof(KEY_REC) * sizeof(long));
-
- if (zb < sizeof(KEY_REC) * sizeof(long))
- zb = sizeof(KEY_REC) * sizeof(long);
-
- DEBUG ("--Checking for memory to support buffer size zb=%ld\n", zb);
- testb = make_buf ((2 * NMERGE + 2) * zb);
- free (testb);
-
- return (zb);
- }
-
-
- /* function to set the parser options based on a character string ...
- * 'a' turns on keep_all_punct, 'e' turns on keep_embedded_punct,
- * and 's' turns on keep_special_chars
- */
-
- void set_parser_options (str)
- char *str;
- {
- extern int keep_all_punct, keep_embedded_punct, keep_special_chars;
-
- while (*str)
- {
- if (*str == 'a')
- {
- keep_all_punct = TRUE;
- printf ("All punctuation characters will be kept.\n");
- }
- else if (*str == 'e')
- {
- keep_embedded_punct = TRUE;
- printf ("Embedded punctuation characters will be kept.\n");
- }
- else if (*str == 's')
- {
- keep_special_chars = TRUE;
- printf ("Special characters will be kept.\n");
- }
-
- ++str;
- }
- }
-
-
-
- /* function to check for user interruption of operations (for use in the
- * Macintosh version of this program only) ... call SystemTask() to give
- * desk accessories a bit of time, and then check for non-null events
- * with GetNextEvent() ... if something comes along (a mouse click, or key
- * press, or whatever) let the user choose to exit the program or to
- * carry on ....
- */
-
- #ifdef LIGHTSPEED
-
- void check_interrupt ()
- {
- EventRecord myEvent;
- char cmd[256], *gets();
- void exit();
-
- SystemTask ();
- if (GetNextEvent (everyEvent, &myEvent))
- {
- fprintf (stderr, "\Quit indexing now?\n");
- gets (cmd);
- if (cmd[0] == 'y' || cmd[0] == 'Y')
- exit (0);
- }
- }
-
- #endif
-
-
- /* a very simple function to return the size of a file ... leave the file
- * pointer positioned at wherever it was when the routine was entered....
- * should work fine with stdio methods, but not guaranteed compatible when
- * mixed in with Mac-specific FSRead() function!
- */
-
- long file_size (fp)
- FILE *fp;
- {
- long fpos, result, ftell();
-
- DEBUG ("--finding the size of file fp=%ld\n", fp);
- fpos = ftell (fp);
- fseek (fp, 0L, 2);
- result = ftell (fp);
- fseek (fp, fpos, 0);
- return (result);
- }
-
-
-
-